package com.teemo.dp.prototype.demo3;

import cn.hutool.json.JSONUtil;
import lombok.Data;

import java.io.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Teemo
 */
@Data
public class Resume implements Cloneable ,Serializable{
    private String name;
    private String sex;
    private Integer age;
    private List<String> hobbies;
    private ExtInfo extInfo;
    private List<String> skills;
    private List<String> experiences;
    private List<String> educations;
    private List<String> projects;
    private List<String> certifications;
    private List<String> languages;
    private List<String> publications;


    @Override
    public Resume clone() throws CloneNotSupportedException {
//        return getResume1();
//        return getResume2();
        return getResume3();

    }

    private Resume getResume3() {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            Resume o = (Resume) ois.readObject();
            return o;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private Resume getResume2() {
        String jsonStr = JSONUtil.toJsonStr(this);
        System.out.println(jsonStr);
        Resume bean = JSONUtil.toBean(jsonStr, Resume.class);
        return bean;
    }

    private Resume getResume1() throws CloneNotSupportedException {
        Resume clone = (Resume) super.clone();
        if (null != skills) {
//            List<String > skis = new ArrayList<>();
//            for (String skill : skills) {
//                skis.add(skill);
//            }
            List<String> skis = skills.stream().collect(Collectors.toList());
            clone.setSkills(skis);
        }
        clone.setExtInfo(extInfo.clone());
        return clone;
    }
}
